package com.iwhalecloud.bss.kite.cucc.order.flow.handles;

import com.iwhalecloud.bss.kite.common.constant.OrderFlowConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.cucc.order.flow.factory.WqConstructFactory;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.Flow;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.WqAccept;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.WqConstruct;
import com.iwhalecloud.bss.kite.dataservice.mapper.flow.WqAcceptMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.flow.WqConstractMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemMapper;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import com.ztesoft.bss.base.staticdata.local.CoreDataInteractLocal;
import com.ztesoft.bss.common.util.SpringUtil;
import tk.mybatis.mapper.entity.Example;

/**
 * @author yu.baocheng@iwhalecloud.com<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020/3/7 2:11 上午 <br>
 * @since ZSmart_BSS_COC_R3.1<br>
 * <Description>
 * 新开通环节处理
 * 该处理器完成如下的功能：
 * 1向服务开通派单。
 * 2在不满足派单条件的时候，转开通睡眠。 在江西，因为服务开通支持订单分解后移，可以不考虑这种情况。
 * 3.营业工单关联处理：当一个业务被派到服务开通环节的时候，如果该产品实例的前序单存在开通差错处理请求，缓装请求和开装请求等，那么CRM进行回复处理。
 * 4.营业工单的主要信息包括：{
 *      1.工单标识.
 *      2.工单类型(1.开通差错，2开装，3缓装， 4质检)
 *      3.销售品实例
 *      4.订单项
 *      5.状态
 *      6.处理类型
 *      7.处理订单项
 *      8.处理工号
 *      9.创建时间
 *      10.处理时间
 * }
 * <br>
 */
public class ProduceTacheHandler extends DefaultTacheHandler {

    public String execute(Flow flow) {
        /*
         * 1.修改order_item表的状态为开通中，状态时间为当前时间。 --由DefaultTacheHandle.after()完成
         * 2.生成开通队列信息(WQ_CONSTRUCT)。
         * 3.营业工单回复处理
         */
        String tacheId = flow.getTacheId();
        String orderItemId = flow.getInstId();
        String custId = flow.getCustId();

        //插入服务开通队列表
        if (!(OrderFlowConsts.FLOW_STATUS_SLEEPED.equals(flow.getStatus()))) {
            String inst_type = flow.getInstType();
            String inst_id = flow.getInstId();
            if (inst_type.equals(OrderFlowConsts.FLOW_INST_TYPE_ORDITEM)) //服务订单才进队列
            {
                WqConstruct wqConstruct = WqConstructFactory.getWqContract(inst_id, flow.getTacheId(), custId);
//                HashMap mapWqConstruct = (HashMap) wqConstructVo.unloadToHashMap();
//                BusiUtils.getDAO(BusiConfigTables.WQ_CONSTRUCT).insert(mapWqConstruct);
                WqConstractMapper wqConstractMapper = SpringUtil.getBean(WqConstractMapper.class);
                wqConstractMapper.insert(wqConstruct);
            }
        }

        //营业工单回复处理
        this.responseAcceptWorkOrder(orderItemId, custId);
        //
        return tacheId;
    }

    public void responseAcceptWorkOrder(String orderItemId, String custId) {
        /*
         * 1.如果本单的受理性质为”修正/撤单/受理/客户确认无资源代装”，那么回复本销售品实例所有工单类型为“开通差错”, “开装”, “缓装”的营业工单:
         *      1.1.状态为：已处理，
         *      1.2.处理类型为”修正/撤单/受理”;
         *      1.3.处理订单项为orderItemId. 1.4.处理时间为当前时间。
         * 2.如果本单的受理单性质为”缓装确认/开装确认/开通差错回复”，那么回复原单的的所有工单类型为” 缓装/开装/开通差错”的营业工单:
         *      2.1.状态为：已处理，
         *      2.2.处理类型为：”缓装确认/开装确认/开通差错回复”;
         *      2.3.处理订单项为orderItemId.
         *      2.4.处理时间为当前时间。
         */

        // 根据order_item_id获取订单信息
        OrderItemMapper orderItemMapper = SpringUtil.getBean(OrderItemMapper.class);
        OrderItem orderItem = orderItemMapper.getOrderItemByOrderItemIdAndCustId(orderItemId, custId);
        String operType = orderItem.getOperType();
//        String oldOrderItemId = orderItem.getOldOrderItemId();
//        String cust_id = order_item_info.getOrderItem().getBelong_cust_id();
        WqAccept wqAccept = new WqAccept();
//        List list_accept_queue = new ArrayList();
//        String update_sql = "update wq_accept set deal_flag = ?, deal_type = ?, deal_order_item_id = ?, deal_date = sysdate " + " where 1=1 ";

//        "update wq_accept set deal_flag = ?, deal_type = ?, deal_order_item_id = ?, deal_date = " + DatabaseFunction.current() + " where 1=1 and cust_id = ? ";

//        String update_sql = SF.orderSql("PRODUCETACHEHANDLER_update_sql");
//        list_param.add("1");
//        list_param.add(order_item_prop);
//        list_param.add(order_item_id);
//        //TODO 增加cust_id
//        list_param.add(cust_id);
        //list_param.add(DAOUtils.getDBCurrentTime());
        wqAccept.setDealFlag("1");
        wqAccept.setDealType(orderItem.getOperType());
        wqAccept.setDealOrderItemId(orderItem.getOrderItemId());
        wqAccept.setDealDate(KiteDateUtils.getCurrentDate());

        WqAcceptMapper wqAcceptMapper = SpringUtil.getBean(WqAcceptMapper.class);
        Example wqAcceptExample = new Example(WqAccept.class);
        Example.Criteria wqAcceptExampleCriteria = wqAcceptExample.createCriteria();

        wqAcceptExampleCriteria.andEqualTo("custId", custId);

        if (CoreDataInteract.EVT_C_0003_1000.equals(operType) || CoreDataInteractLocal.EVT_C_0003_2200.equals(operType)) { //如果本单的受理性质为”修正/撤单/受理/客户确认无资源代装”
            //modify by AyaKoizumi 110421
//            list_param.add(order_item_id);
//            wqAcceptDao.update(update_sql + " and order_item_id = ?", list_param);
            wqAcceptExampleCriteria.andEqualTo("orderItemId", orderItem.getOrderItemId());
        }
        else if (CoreDataInteract.EVT_C_0003_1100.equals(operType) || CoreDataInteract.EVT_C_0003_1200.equals(operType)) {
//            list_param.add(old_order_item_id);
//            wqAcceptDao.update(update_sql + " and order_item_id = ?", list_param);
            wqAcceptExampleCriteria.andEqualTo("orderItemId", orderItem.getOldOrderItemId());
        }
        else if (CoreDataInteract.EVT_C_0003_1801.equals(operType) || CoreDataInteract.EVT_C_0003_1500
                .equals(operType) || CoreDataInteractLocal.EVT_C_0003_2300
                .equals(operType)) { //如果本单的受理单性质为”缓装确认/开装确认/开通差错回复”
            //modify by AyaKoizumi 110421
//            list_param.add(old_order_item_id);
//            wqAcceptDao.update(update_sql + " and order_item_id = ?", list_param);
            wqAcceptExampleCriteria.andEqualTo("orderItemId", orderItem.getOldOrderItemId());
        }
        wqAcceptMapper.updateByExampleSelective(wqAccept, wqAcceptExample);
    }

}
